<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand"/>
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
    <title>课程-个人任务-发布</title>

    <%@ include file="/common/commonLib.jsp" %>
    <link rel="stylesheet" type="text/css" href="<c:url value='/css/taskPublish.css'/>"/>
    <link rel="stylesheet" type="text/css" href="<c:url value='/css/common-main.css'/>"/>
    <script src="<c:url value='/js/taskPersonalPublish.js'/>" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="container-fluid main">
    <div class="row">
        <div class="col-md-12">
            <%@include file="/common/header.jsp" %>
        </div>
        <div class="col-md-12">
            <%@include file="common/courseHeader.jsp" %>
        </div>
        <div class="col-md-12">
            <div class="row">
                <div class="col-md-3">
                    <%@include file="common/courseLeft.jsp" %>
                </div>
                <div class="col-md-9">
                    <div class="row">
                        <div class="col-md-12">
                            <%@include file="common/courseNavbar.jsp" %>
                        </div>
                        <div class="col-md-12">
                            <div class="tab-nav">
                                <a id="personal-task-pub-href1" class="front-nav">任务首页
                                </a>&nbsp;>&nbsp;<a id="personal-task-pub-href2" class="front-nav">任务详情
                            </a>&nbsp;>&nbsp;<span class="back-nav">任务分配情况</span>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="row publish-personal">
                                <div class="col-md-12">
                                    <input type="button" id="new-personal-task-publish" class="btn btn-primary"
                                           value="分配任务">
                                </div>
                                <div class="col-md-12">
                                    <!-- 已分配任务记录 -->
                                    <label for="">任务分配记录</label>
                                    <input type="button" id="select-all" class="btn btn-info" value="全选">
                                    <input type="button" id="select-all-publish" class="btn btn-primary"
                                           onclick="batchUpdateAnswer()" value="公布答案">

                                    <ul id="task-pub-record-personal">

                                    </ul>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 添加任务分配对象 -->
<div id="add-personal-publish-object">
    <div class="row">
        <div class="col-xs-12">
            <div class="form-group">
                <div class="row">
                    <div class="col-xs-1"></div>
                    <div class="col-xs-2">
                        <label for="">选择分配角色</label>
                    </div>
                    <div class="col-xs-4">
                        <select id="selectType" class="form-control" onclick="selectTaskType(this)">
                            <option value="err" selected="selected">--选择分配对象--</option>
                            <option value="STUDENT">学生</option>
                            <option value="ASSISTANT">助教</option>
                            <option value="CLASS">小组</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <!-- 如果是学生/助教  +++ 搜索 -->
        <div class="col-xs-12" id="stu-assi-search">
            <div class="row">
                <div class="form-group">
                    <div class="col-xs-1"></div>
                    <div class="col-xs-2">
                        <label for="">查找分配对象</label>
                    </div>
                    <div class="col-xs-4">
                        <input type="text" name="keyword" class="form-control">
                    </div>
                    <button type="button" id="searchButton" class="btn btn-primary">搜索</button>
                </div>
            </div>
        </div>

        <!-- 搜索得到的学生/助教/小班列表 -->
        <div class="col-xs-12" id="search-list">
            <div class="row">
                <div class="form-group">
                    <div class="col-xs-1"></div>
                    <div class="col-xs-2">
                        <label for="">查找结果</label>
                    </div>
                    <div class="col-xs-8">
                        <table class="table table-hover">
                            <tbody id="searchList">

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- 已添加的学生/助教/小组列表 -->
        <div class="col-xs-12" id="had-add-list">
            <div class="row">
                <div class="form-group">
                    <div class="col-xs-1"></div>
                    <div class="col-xs-2">
                        <label for="">已添加对象</label>
                    </div>
                    <div class="col-xs-8">
                        <table class="table table-hover">
                            <tbody id="chooseList">

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- “发布”按钮 -->
        <div class="col-xs-12" style="text-align:center;margin-bottom:4rem;">
            <button type="button" id="personalTaskPublish" class="btn btn-primary">立即发布</button>
        </div>

    </div>
</div>
<%@include file="/common/footer.jsp" %>
</body>
<script>
    var data =${data};
    var courseId;
    var taskId;
    var searchs = [];
    var chooses = [];
    $(document).ready(function () {
        if (data.status != "success") {
            alert(data.message);
            return;
        }
        $("#task").css("color", "#00b43c");
        courseId = data.data.courseId;
        taskId = data.data.taskId;
        changeUrl(data.data.courseId);
        $("#searchButton").click(search);
        $("#personalTaskPublish").click(publishTask);
        fetchCourse(data.data.courseId);
        initAssignList(data.data);

        $("#personal-task-pub-href1").attr("href", CONTEXT_PATH + "task/list?courseId=" + data.data.courseId);
        $("#personal-task-pub-href2").attr("href", CONTEXT_PATH + "task/detail?id=" + data.data.taskId);
    });

    var batchUpdateAnswer = function () {
        var ids = [];
        $('input[name="ids"]:checked').each(function () {
            ids.push($(this).val());
        });
        if (ids.length <= 0) {
            alert("请选择公布答案的对象");
            return;
        }
        ;
        var json = {
            list: ids,
            isAnswerVisible: "YES"
        };
        $.post(CONTEXT_PATH + "taskSubmitResult/batchDealAnswerVisible", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                window.location.reload();
            } else {
                alert(data.message);
            }
        });

    }

    var updateAnswer = function (id, status) {

        var json = {
            id: id,
            isAnswerVisible: status
        };
        $.post(CONTEXT_PATH + "taskSubmitResult/dealAnswerVisible", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                window.location.reload();
            } else {
                alert(data.message);
            }
        });
    }

    var fetchAssign = function (json) {
        $.get(CONTEXT_PATH + "taskSubmitResult/ajaxPtListData", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                initAssignList(data.data);
            } else {
                alert(data.message);
            }
        });
    }

    var initAssignList = function (data) {
        var list = $("#task-pub-record-personal");
        list.empty();
        for (var i = 0; i < data.list.length; i++) {
            var item = data.list[i];
            var ucode = "---";
            if (item.roleType == "STUDENT") {
                ucode = item.ucode;
            }
            ;
            if (item.roleType == "ASSISTANT") {
                ucode = "";
            }
            ;
            var s = "";
            s += "<li >";
            s += "<input type=\"checkbox\" name=\"ids\" class=\"check\" value=\"" + item.id + "\" >";
            if (item.status == "UNCOMMITTED") {
                s += "<span onclick=\"uncommitted()\" title=\"" + getTypeStr(item.roleType) + "  " + ucode + " " + item.assignedName + " 未提交\">" + getTypeStr(item.roleType) + "  " + ucode + " " + item.assignedName + " 未提交</span>";
            } else if (item.status == "COMMITTED") {
                s += "<span onclick=\"detail(" + item.id + ")\" title=\"" + getTypeStr(item.roleType) + "  " + ucode + " " + item.assignedName + " 已提交\">" + getTypeStr(item.roleType) + "  " + ucode + " " + item.assignedName + "  已提交</span>";
            } else {
                s += "<span onclick=\"detail(" + item.id + ")\" title=\"" + getTypeStr(item.roleType) + "  " + ucode + " " + item.assignedName + " 已批改\">" + getTypeStr(item.roleType) + "  " + ucode + " " + item.assignedName + "  已批改</span>";
            }
            ;
            if (item.isAnswerShow == "YES") {
                s += "<input onclick=\"updateAnswer(" + item.id + ",'NO')\" type=\"button\" id=\"\" class=\"btn btn-success publish-answer\" value=\"取消公布\">";
            } else {
                s += "<input onclick=\"updateAnswer(" + item.id + ",'YES')\" type=\"button\" id=\"\" class=\"btn btn-info publish-answer\" value=\"公布答案\">";
            }

            s += "<input type=\"button\" onclick=\"delete1(" + item.id + ") \" class=\"btn btn-danger publish-answer\" value=\"删除\">";

            s += "</li>";
            list.append(s);
        }
    }

    function delete1(id) {
        console.log("id:::" + id);
        if (window.confirm("您确定要删除吗?")) {
            $.post(CONTEXT_PATH + "taskSubmitResult/ajaxDelete", "id=" + id, function (data) {
                if (data.status == "success") {
                    layer.msg("删除成功");
                    location.reload();
                } else {
                    layer.msg("删除失败");
                }
            });
        }

    }


    var detail = function (id) {
        window.location.href = CONTEXT_PATH + "taskSubmitResult/detail?id=" + id;
    }
    var uncommitted = function () {
        alert("该学生还未提交任务！");
    }
    //searchButton
    var search = function () {
        var type = $("#selectType option:selected").val();
        var keyword = $("input[name='keyword']").val();
        var taskId = data.data.taskId;
        if (isEmpty(keyword)) {
            alert("请输入搜索信息");
            return false;
        }
        if (type == "STUDENT" || type == "ASSISTANT") {
            var json = {
                keywords: keyword,
                courseId: courseId,
                taskId: taskId,
                type: type

            };
            $.get(CONTEXT_PATH + "taskSubmitResult/searchUser", {
                data: JSON.stringify(json)
            }, function (data) {
                if (data.status == "success") {
                    searchs = data.data.list;
                    initSearchList(data.data.list, data.data.type);
                } else {
                    alert(data.message);
                }
            });
        } else {
            alert("请选择类型");
            return;
        }
    }

    var getClass = function () {
        var json = {
            courseId: courseId,
            classType: "TASKTEAM"
        };
        $.get(CONTEXT_PATH + "class/ajaxListData", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                searchs = data.data.list;
                initSearchList(data.data.list, "CLASS");
            } else {
                alert(data.message);
            }
        });
    }

    initSearchList = function (list, type) {
        var searchList = $("#searchList");
        searchList.empty();
        if (list.length == 0) {
            searchList.append("无搜索结果！");
        }
        for (var i = 0; i < list.length; i++) {
            var s = "";
            s += "<tr>";
            if (type == "STUDENT") {
                s += "<td title=\"" + list[i].studentCode + "\">" + list[i].studentCode + "</td>";
                s += "<td title=\"" + list[i].name + "\">" + list[i].name + "</td>";
                s += "<td title=\"学生\">学生</td>";
                if (contain(list[i], "STUDENT")) {
                    s += "<td><span title=\"已选择\">已选择</span></td>";
                } else {
                    s += "<td><span onclick=\"addChooses(" + i + ",'STUDENT',this)\">添加</span></td>";
                }
            } else if (type == "ASSISTANT") {
                s += "<td title=\"" + list[i].assistantCode + "\">" + list[i].assistantCode + "</td>";
                s += "<td title=\"" + list[i].name + "\">" + list[i].name + "</td>";
                s += "<td title=\"助教\">助教</td>";
                if (contain(list[i], "ASSISTANT")) {
                    s += "<td><span>已选择</span></td>";
                } else {
                    s += "<td><span onclick=\"addChooses(" + i + ",'ASSISTANT',this)\">添加</span></td>";
                }
            } else {
                s += "<td></td>";
                s += "<td title=\"" + list[i].name + "\">" + list[i].name + "</td>";
                s += "<td title=\"小组\">小组</td>";
                if (contain(list[i], "CLASS")) {
                    s += "<td><span>已选择</span></td>";
                } else {
                    s += "<td><span onclick=\"addChooses(" + i + ",'CLASS',this)\">添加</span></td>";
                }
            }
            s += "</tr>";
            searchList.append(s);
        }
    }

    var getTypeStr = function (type) {
        if (type == "STUDENT") {
            return "学生";
        } else if (type == "ASSISTANT") {
            return "助教";
        } else {
            return "小组";
        }
    }

    var addChooses = function (i, type, obj) {
        var item = searchs[i];
        var json = {
            id: item.id,
            type: type
        }
        chooses.push(json);
        var chooseList = $("#chooseList");
        var s = "<tr>";
        s += "<td class=\"name\" title=\"" + item.name + "\">" + item.name + "</td>"
        s += "<td class=\"type\" title=\"" + getTypeStr(type) + "\">" + getTypeStr(type) + "</td>";
        s += "</tr>";
        chooseList.append(s);
        $(obj).parent("td").parent("tr").remove();

    }

    var contain = function (item, type) {
        for (var i = 0; i < chooses.length; i++) {
            if (chooses[i].id == item.id && chooses[i].type == type) {
                return true;
            }
        }
        return false;
    }


    var isEmpty = function (s) {
        if (s == null || s == "" || s == undefined) {
            return true;
        }
        return false;
    }

    var publishTask = function () {
        var param = [];
        for (var i = 0; i < chooses.length; i++) {
            var item = {
                assignId: chooses[i].id,
                type: chooses[i].type
            };
            param.push(item);
        }
        var json = {
            taskId: taskId,
            params: param
        };
        $.post(CONTEXT_PATH + "taskSubmitResult/batchCreate", {
            data: JSON.stringify(json)
        }, function (data) {
            if (data.status == "success") {
                window.location.reload();
            } else {
                alert(data.message);
            }
        });
    };

</script>
</html>
